package com.jsbj.user.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.tools.ContextUtil;
import com.jsbj.tools.DateUtils;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysEquFieldConfig;
import com.jsbj.user.mapper.SysEquFieldConfigMapper;
import com.jsbj.user.service.ISysEquFieldConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2023-01-04
 */
@Service
public class SysEquFieldConfigServiceImpl extends ServiceImpl<SysEquFieldConfigMapper, SysEquFieldConfig> implements ISysEquFieldConfigService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public List<SysEquFieldConfig> getByCondition(JSONObject object) {
        String userId = RequestHeaderUtils.getUserId(request);
        String tenantCode = object.getString("tenantCode");
        String secCode = object.getString("secCode");
        String systemCode = object.getString("systemCode");
        String equTypeValue = object.getString("equTypeValue");
        String type = object.getString("type");
        if (StringUtils.isEmpty(tenantCode) || StringUtils.isEmpty(secCode) || StringUtils.isEmpty(systemCode) || StringUtils.isEmpty(equTypeValue)){
            return null;
        }
        if(StringUtils.isEmpty(type)){
            type = "1";
        }
        //先查询设备默认字段列表，再加入特殊字段列表
        List<SysEquFieldConfig> sysEquFieldConfigs = queryListBySysCodeAndEquType("0","0",type);
        //特殊配置字段列表
        List<SysEquFieldConfig>  sysEquFieldConfigT = queryListBySysCodeAndEquType(systemCode,equTypeValue,type);
        sysEquFieldConfigs.addAll(sysEquFieldConfigT);

        //调用接口的入参
        JSONObject param = new JSONObject();
        param.put("tenantCode",tenantCode);
        param.put("orgCode",secCode);
        param.put("systemCode",systemCode);
        param.put("userId",userId);
        for (int i=0;i<sysEquFieldConfigs.size();i++){
            //日期控件，默认当天
            if("date".equals(sysEquFieldConfigs.get(i).getFieldType())){
                String today = DateUtils.getDate();
                sysEquFieldConfigs.get(i).setDefalutValue(today);
            }
            //新增时默认全部都能修改
            sysEquFieldConfigs.get(i).setIsEditable(0);
            //下拉框控件，查询是否需要通过方法调用
            if("select".equals(sysEquFieldConfigs.get(i).getFieldType())){
                //查询下拉框数据列表是否有值，有值则转成对象
                List<Map> selectDataList = new ArrayList();
                if(StringUtils.isNotEmpty(sysEquFieldConfigs.get(i).getSelectDatas())){
                    selectDataList = JSONArray.parseArray(sysEquFieldConfigs.get(i).getSelectDatas(),Map.class);
                }else if(sysEquFieldConfigs.get(i).getIsFromInterface()==1){ //通过接口查询下拉列表
                    String className = sysEquFieldConfigs.get(i).getInterfaceClassName();
                    String methodName = sysEquFieldConfigs.get(i).getInterfaceMethodName();
                    selectDataList = ContextUtil.getFromClassMethod(className,methodName,param);
                }
                sysEquFieldConfigs.get(i).setSelectDataList(selectDataList);
                //字段若是必填，则下拉框默认第一个值
                if(sysEquFieldConfigs.get(i).getFieldRequired()==1){
                    if(StringUtils.isEmpty(sysEquFieldConfigs.get(i).getDefalutValue())){
                        sysEquFieldConfigs.get(i).setDefalutValue(selectDataList==null?"":selectDataList.get(0).get("value").toString());
                    }
                }
            }
        }

        return sysEquFieldConfigs;
    }

    @Override
    public List<SysEquFieldConfig> queryListBySysCodeAndEquType(String systemCode, String equType,String type) {
        LambdaQueryWrapper<SysEquFieldConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysEquFieldConfig::getSystemCode,systemCode);
        wrapper.eq(SysEquFieldConfig::getEquTypeValue,equType);
        wrapper.eq(SysEquFieldConfig::getType,type);
        wrapper.eq(SysEquFieldConfig::getDisabled,0);
        wrapper.orderByAsc(SysEquFieldConfig::getSort);
        return baseMapper.selectList(wrapper);
    }


}
